package com.blb.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.*;
import com.blb.service.ISalaryService;
import com.blb.service.IStaffService;
import com.blb.service.IStaffStatusService;
import com.blb.utils.*;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *人事管理中的员工管理
 * @author zyx
 * @since 2020-05-25
 */
@Controller
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    IStaffService staffService;

    @Autowired
    ISalaryService salaryService;

    @Autowired
    IStaffStatusService staffStatusService;
   @RequiresPermissions("staff")
    @ResponseBody
    @RequestMapping("staff_list")
    public DataTablesPage<Staff> list(Integer draw,Integer start, Integer length,HttpSession model,Staff staff, String staffName){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        //存放部门
        List<Dept> depts = staffService.xiala(user.getId());
        model.setAttribute("dept",depts);
        System.out.println(user.getId());
        //存放身份
        List<StaffStatus> status = staffService.status(user.getId());
        model.setAttribute("status",status);

        //分页
        staff.setStaffName(staffName);
        int count = staffService.staffAll(user.getId());
        List<Staff> like = staffService.staffList(start,length,staff.getStaffName(),user.getId());
        return new DataTablesPage(draw,count,count, like);
    }




    //根据部门职位id获取部门职位
    @RequiresPermissions("staff")
    @ResponseBody
    @RequestMapping("postZhi")
    List<Post> postZhi(Model model,String deptId){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Post> postList = staffService.postName(deptId,user.getId());
        model.addAttribute("postList",postList);
        return  postList;
    }
    @RequiresPermissions("staff:add")
    @ResponseBody
    @PostMapping("add")
    Response add(Staff staff, String status){
        return staffService.addAll(staff,status);
    }

    /**
     *员工账号是否存在
     */
    @RequiresPermissions("staff")
    @ResponseBody
    @GetMapping("/isStaffName")
    Response isStaffName(String staffName,String flag){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
            List<Staff> staff2 = staffService.isStaffName(staffName);
            if (staff2.isEmpty()) {
                return Response.success();
            } else {
                return Response.error("员工账号已经存在");
            }

    }
    /**
     * 判断员工编号已经存在
     */
    @RequiresPermissions("staff")
    @ResponseBody
    @GetMapping("/isStaffId")
    Response idStaffId(String staffId){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Staff> staff = staffService.isStaffId(staffId,user.getId());
        if(staff.isEmpty()){
            return Response.success();
        }else {
            return Response.error("员工编号已经存在");
        }
    }

    //修改传值
    @RequiresPermissions("staff:update")
    @RequestMapping("staffUpd")
    public String staffUpd(WebRequest staff,Model model){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Staff list = staffService.staffUpd(staff.getParameter("data"),user.getId());

        List<Salary> salaryList = staffService.salaryList(list.getStaffId());

model.addAttribute("salaryList",salaryList);
        model.addAttribute("list",list);
        return "human/staff/Brand_upd";
    }

    //修改
    @RequiresPermissions("staff:update")
    @ResponseBody
    @RequestMapping("upd")
    Response upd(Staff staff, WebRequest req){
        staffService.updStaffAll(req.getParameter("status"),staff);
        return Response.success();
    }

    //启用禁用
    @RequiresPermissions("staff")
    @ResponseBody
    @RequestMapping("isStart")
    Response isStart(Staff req){
        Staff staff = new Staff();
        System.out.println("嘻嘻哈哈");
        if("0".equals(req.getNumberId())){
            staff.setNumberId("1");
            staff.setStaffId(req.getStaffId());
            staffService.updStatus(staff);
        }else{
            staff.setNumberId("0");
            staff.setStaffId(req.getStaffId());
            staffService.updStatus(staff);
        }
        return Response.success();
    }

}

